<template>
	<!-- 客户编辑 -->
	<div v-if="!is_password">
		<div class="edit-top">
			编辑信息
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<div class="edit-title">客户信息</div>
			<div class="edit-item">
				<label>客户名称</label><input v-model="info.chineseName" type="text" />
			</div>
			<div class="edit-item">
				<label>所属企业</label><select v-model="info.enterpriseId">
					<option v-for="item in enterprise_list" :value="item.id">{{item.enterpriseName}}</option>
				</select>
			</div>
			<div class="edit-item">
				<label>所属角色</label>
				<span v-if="role_list.length <= 0">未设置角色</span>
				<template>
					<el-checkbox-group v-model="roleIds">
						<el-checkbox v-for='item in role_list' :key='item.id' :label="item.id">{{item.roleName}}</el-checkbox>
					</el-checkbox-group>
				</template>
			</div>
			<div class="edit-item">
				<label>邮箱</label><input v-model="info.email" type="text" />
			</div>
			<div class="edit-item">
				<label>证件类型</label>
				<el-radio v-for='item in dict.identityType' :key='item.value' v-model="identityType" :label="item.value">{{item.type}}</el-radio>
			</div>
			<!-- <div class="edit-item">
				<label>客户证件类型</label><select v-model="info.identityType">
					<option v-for='item in dict.identityType' :value="item.value">{{item.type}}</option>
				</select>
			</div> -->
			<div class="edit-item">
				<label>客户证件号码</label><input v-model="info.identityNum" type="text" />
			</div>
			<div class="edit-item">
				<label>联系手机</label><input v-model="info.mobileNum" type="text" />
			</div>
			<div class="edit-title">用户信息</div>
			<div class="edit-item">
				<label>用户名</label><input v-model="sysUserDto.userName" type="text" />
			</div>
			<div class="edit-item">
				<label>密码</label><el-button @click='is_password = !is_password' type="primary">修改密码</el-button>
			</div>
			<!-- <div class="edit-item">
				<label>用户类型</label><select v-model="sysUserDto.userType">
					<option v-for='item in dict.userType' :value="item.value">{{item.type}}</option>
				</select>
			</div> -->
			<div class="edit-item">
				<label>用户状态</label><select v-model="sysUserDto.userStatus">
					<option v-for='item in dict.userStatus' :value="item.value">{{item.type}}</option>
				</select>
			</div>
			<div class="edit-item">
				<label>数据权限</label>
				<el-radio v-for='item in dict.dataAuth' :key='item.value' v-model="sysUserDto.dataAuth" :label="item.value">{{item.type}}</el-radio>
			</div>
		</div>
		<div class="edit-bottom">
			<span @click='cancel'>取消</span>
			<el-button @click='save' type="primary">保存</el-button>
		</div>
	</div>
	<div v-else>
		<div class="edit-top">
			修改密码
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<div class="edit-item">
				<label>原密码</label><input v-model="password_info.oldPassword"  type="password" />
			</div>
			<div class="edit-item">
				<label>新密码</label><input v-model="password_info.newPassword"  type="password" />
			</div>
			<div class="edit-item">
				<label>确认密码</label><input v-model="password_info.newPassword1"  type="password" />
			</div>
		</div>
		<div class="edit-bottom">
			<span @click='is_password = !is_password'>取消</span>
			<el-button @click='save_password' type="primary">保存</el-button>
		</div>
	</div>
</template>

<script>
	let _this;
	export default {
		props: ['id'],
		data() {
			return {
				password_info:{
					oldPassword:'',
					newPassword:'',
					newPassword1:'',
					sysUserId:'',
				},
				is_password:false,
				dict: '',
				enterprise_list: [],
				role_list: [],
				roleIds:[],
				identityType:"",
				info: {
					enterpriseId: '',
					email: '',
					"identityNum": "",
					mobileNum: '',
					idCard: '',
					jobStatus: 0,
					roleIds: [],
					
				},
				sysUserDto:{userName:'',dataAuth:'0',userStatus:0,id:''}
			}
		},
		created() {
			_this = this;
		},
		mounted() {

			this.get_dict();
			this.get_enterprise_list();
			this.get_role_list();
		},
		methods: {
			save_password(){
				if(this.password_info.oldPassword == ''){
					this.$popup.open('原密码不能为空');
					return;
				}
				if(this.password_info.newPassword == ''){
					this.$popup.open('新密码不能为空');
					return;
				}
				if(this.password_info.newPassword != this.password_info.newPassword1){
					this.$popup.open('两次新密码不相同');
					return;
				}
				this.password_info.sysUserId = this.info.userId;
				this.$axios.post(this.$global.serverIp + 'admin/sysUser/changeUserPassword', this.password_info)
					.then(function(res) {
						if (res != '') {
							_this.$popup.open('修改成功');
							_this.is_password = !_this.is_password;
							_this.password_info.oldPassword = '';
							_this.password_info.newPassword = '';
							_this.password_info.newPassword1 = '';
							
						}
					})
			},
			get_info() {
				this.$axios.get(this.$global.serverIp + 'admin/sysClient/detail/' + this.id)
					.then(function(res) {
						if (res != '') {
							_this.info = res.data;
							_this.identityType = _this.info.identityType+'';
							_this.sysUserDto.dataAuth = _this.info.dataAuth+"";
							_this.sysUserDto.userName = _this.info.userName;
							_this.sysUserDto.userStatus = _this.info.userStatus;
							_this.sysUserDto.id = _this.info.userId;
							var a = [];
							for (var i = 0; i < _this.info.roles.length; i++) {
								a.push(_this.info.roles[i].id);
							}
							_this.roleIds = a;
						}
					})

			},
			get_dict() {
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=sysClient')
					.then(function(res) {
						if (res != '')
							_this.dict = res.data;
					})

			},
			get_enterprise_list() {
				this.$axios.post(this.$global.serverIp + 'admin/sysEnterprise/findPage', {
						"limit": "10000",
						"page": "1"
					})
					.then(function(res) {
						if (res != '') {
							_this.enterprise_list = res.data.list;
							_this.info.enterpriseId = _this.enterprise_list[0].id;
							_this.get_info();
							
							
						}

					})
			},
			get_role_list() {
				this.$axios.post(this.$global.serverIp + 'admin/sysRole/findPage', {
						"limit": "10000",
						"page": "1"
					})
					.then(function(res) {
						if (res != '') {
							_this.role_list = res.data.list;
						}

					})
			},
			change_data(list) {
				var data = [];
				var length = list.length;
				for (var i = 0; i < length; i++) {
					var e = {};
					e.id = list[i].id;
					e.label = list[i].roleName;
					data.push(e);
					this.opendata.push(list[i].id);
				}
				return data;
			},

			save() {
				if (this.info.chineseName == "") {
					this.$popup.open('人员姓名不能为空');
					return;
				}
				if (this.roleIds.length <= 0) {
					this.$popup.open('所属角色不能为空');
					return;
				}
				if (this.info.mobileNum == '') {
					this.$popup.open('联系手机不能为空');
					return;
				}
				if (this.info.identityNum == '') {
					this.$popup.open('证件号不能为空');
					return;
				}
				this.info.sysUserDto = this.sysUserDto;
				this.info.roleIds = this.roleIds;
				this.info.sex = this.sex;
				this.$axios.post(this.$global.serverIp + 'jianeng-admin/sysClient/update', _this.info)
					.then(function(res) {
						if (res != '') {
							_this.success('编辑成功');
						}
					})
			},
			success(content) {
				this.$alert(content, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						_this.$emit("saveEdit", 2);
					}
				});
			},
			
			cancel() {
				this.$emit("saveEdit", 1);
			}
		}
	}
	import '@/assets/css/edit.css'
</script>
